package org.vt.ece3574.pictoslider.facebook;

import org.vt.ece3574.pictoslider.CongratulationsPopUpWindow;
import org.vt.ece3574.pictoslider.GameScreen;
import org.vt.ece3574.pictoslider.HighScoreObject;

import android.content.Intent;
import android.os.Bundle;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;

/**
 * This class is used to manage all facebook events for the app.
 * 
 * @author Michael
 */
public class FacebookManager {

	/**
	 * This application's ID on Facebook.
	 */
	public static final String APP_ID = "398326406845027";

	/**
	 * The Link to post every time the user posts to facebook.
	 */
	private static final String WEB_URL = "http://code.google.com/p/pictoslider/";

	/**
	 * The message to post to facebook. Part 1 means it comes before the
	 * difficulty for the puzzle.
	 */
	private static final String MESSAGE_PART1 = "I just solved a ";
	/**
	 * The message to post to facebook. Part 2 means it comes after the
	 * difficulty, but before the solve time for the puzzle.
	 */
	private static final String MESSAGE_PART2 = " difficulty puzzle in ";
	/**
	 * The message to post to facebook. Part 3 means it comes after the solve
	 * time for the puzzle, but before the number of moves.
	 */
	private static final String MESSAGE_PART3 = " using only ";
	/**
	 * The message to post to facebook. Part 4 means it comes after the solve
	 * time and number of moves for the puzzle.
	 */
	private static final String MESSAGE_PART4 = " moves on PictoSlider!";

	/**
	 * String key used in a bundle passed to the Facebook API. This key is used
	 * for the message under the link in the post.
	 */
	private static final String MESSAGE_KEY = "description";
	/**
	 * String key used in a bundle passed to the Facebook API. This key is used
	 * for the link which is posted.
	 */
	private static final String WEB_URL_KEY = "link";
	/**
	 * String used to tell the Facebook API what kind of action you want to do.
	 */
	private static final String FB_DIALOG_COMMAND = "feed";

	/**
	 * The reference to the Facebook object for this manager.
	 */
	private Facebook facebook;
	/**
	 * The reference to the asynchronous facebook runner for this manager.
	 */
	private AsyncFacebookRunner runner;
	/**
	 * The manager's parent activity. It is needed by the Facebook API.
	 */
	private CongratulationsPopUpWindow parentActivity;

	/**
	 * Constructor for this class.
	 * 
	 * @param a
	 *            - The manager's parent activity.
	 */
	public FacebookManager(CongratulationsPopUpWindow a) {
		parentActivity = a;
		facebook = new Facebook(APP_ID);
		runner = new AsyncFacebookRunner(facebook);
	}

	/**
	 * Starts a new Facebook posting action. Launches a facebook dialog for
	 * posting with a default message and link, and allows the user to add his
	 * or her own comments to the post.
	 */
	public void post() {
		if (!facebook.isSessionValid()) {
			facebook.authorize(parentActivity, new String[] {},
					new FacebookDialogListener(this, runner));
		}
		Bundle parameters = new Bundle();
		Bundle b = parentActivity.getIntent().getExtras();
		long time = b.getLong(GameScreen.SOLVE_TIME);
		int moves = b.getInt(GameScreen.NUM_SWAPS);
		String difficulty = b.getString(GameScreen.DIFFICULTY);
		HighScoreObject hsObject = new HighScoreObject(0, time, null);
		parameters.putString(MESSAGE_KEY, MESSAGE_PART1 + difficulty
				+ MESSAGE_PART2 + hsObject.getTimeAsString() + MESSAGE_PART3
				+ moves + MESSAGE_PART4);
		parameters.putString(WEB_URL_KEY, WEB_URL);
		facebook.dialog(parentActivity, FB_DIALOG_COMMAND, parameters,
				new FacebookDialogListener(this, runner));

	}

	/**
	 * Calls Facebook.authorizeCallback with the given parameters.
	 * 
	 * @see com.facebook.android.Facebook#authorizeCallback(int, int,
	 *      android.content.Intent)
	 * 
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 */
	public void authorizeCallback(int requestCode, int resultCode, Intent data) {
		facebook.authorizeCallback(requestCode, resultCode, data);
	}

	/**
	 * Let the parent activity know that the posting action has completed.
	 */
	public void notifyPostComplete() {
		parentActivity.notifyPostComplete();
	}
}
